package com.zzh.live

import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import java.lang.Exception

class HaoPlayer : SurfaceHolder.Callback {
    private var dataSource: String? = null
    private var holder: SurfaceHolder? = null

    companion object {
        init {
            System.loadLibrary("native-demo")
        }
    }

    fun setDataSource(dataSource: String) {
        this.dataSource = dataSource
    }

    //开始播放
    fun start() {

    }

    //准备传入地址到ffmpeg
    fun prepare() {
        if (dataSource.isNullOrBlank()) throw Exception("please setDataSource() beforce......")
        prepareNative(dataSource!!)
    }

    //C++ FFmpeg 出现错误，回调此方法
    fun onFFmpegError(errorCode: Int) {
        Log.d("tag_player","onFFmpegError errorCode:$errorCode............")

    }

    //C++ FFmpeg 准备完成时，回调此方法
    fun onFFmpegPrepared() {
        Log.d("tag_player","onFFmpegPrepared............")
    }


    fun stop() {

    }

    fun release() {
        holder?.removeCallback(this)
    }


    fun setSurfaceView(surface: SurfaceView) {
        holder = surface.holder
        holder!!.addCallback(this)
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        Log.d("TAG_PLAYER", "player surfaceCreated----------------")
    }

    //画布变化(横竖屏切换 home建)
    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        Log.d("TAG_PLAYER", "player surfaceChanged----------------")

    }

    //画布销毁
    override fun surfaceDestroyed(holder: SurfaceHolder) {
        Log.d("TAG_PLAYER", "player surfaceDestroyed----------------")

    }

    private external fun prepareNative(dataSource: String)

}